/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.httprequest;

import cn.ac.iie.di.util.HttpResponseParser;
import java.io.IOException;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.log4j.Logger;

public class JettyClient {

        private static Logger logger = Logger.getLogger(JettyClient.class);

        //post
        public String send(String url, String responseString) {
                logger.debug(url + " JettyClient send begin!");
                CloseableHttpClient httpClient = null;
                CloseableHttpResponse response = null;
//        HttpPost httppost = null;
                String rs = "";
                try {
                        httpClient = HttpClients.createDefault();
                        HttpPost httppost = new HttpPost(url);
                        ByteArrayEntity entity = new ByteArrayEntity(responseString.getBytes());
//            InputStreamEntity reqEntity = new InputStreamEntity(new ByteArrayInputStream(responseString.getBytes()), -1);
                        entity.setContentType("binary/octet-stream");
                        entity.setChunked(true);
                        httppost.setEntity(entity);
                        logger.debug(url + " sending!wait for record!");
                        response = httpClient.execute(httppost);
                        logger.debug(url + " sending finished!");
                        StatusLine st = response.getStatusLine();

                        if (st.getStatusCode() == 200) {
                                logger.debug(url + " JettyClient send getStatusCode is 200!");
                                rs = HttpResponseParser.getResponseContent(response);
                        } else {
                                logger.debug(url + " JettyClient send getStatusCode is not 200!");
                                rs = "";
                        }
                } catch (Exception ex) {
                        logger.error(url + " JettyClient send error!" + ex.getMessage() + "|" + ex.getLocalizedMessage());
                        return "";
                } finally {
                        try {
                                if (response != null) {
                                        response.close();
                                }
                        } catch (IOException ex) {
                                logger.error(ex.getMessage(), ex);
                        }
                        try {
                                if (httpClient != null) {
                                        httpClient.close();
                                }
                        } catch (IOException ex) {
                                logger.error(ex.getMessage(), ex);
                        }
                        logger.debug(url + " JettyClient send end!");
                }
                return rs;
        }

        //get提交，返回文件
        public String sendReturnFile(String url) {
                logger.debug(url + " JettyClient send begin!");
                String rs = "";
                CloseableHttpClient httpClient = null;
                CloseableHttpResponse response = null;
                try {
                        httpClient = HttpClients.createDefault();

                        HttpGet httpget = new HttpGet(url);
                        logger.debug(url + " sending!wait for record!");
                        response = httpClient.execute(httpget);
                        logger.debug(url + " sending finished!");
                        StatusLine st = response.getStatusLine();
                        if (st.getStatusCode() == 200) {
                                logger.debug(url + " JettyClient send getStatusCode is 200!");
                                rs = HttpResponseParser.getResponseFile(response);
                        } else {
                                logger.debug(url + " JettyClient send getStatusCode is not 200!");
                                rs = "";
                        }
                } catch (Exception ex) {
                        logger.error(url + " JettyClient send error!" + ex.getMessage() + "|" + ex.getLocalizedMessage());
                        return rs;
                } finally {
                        try {
                                if (response != null) {
                                        response.close();
                                }
                        } catch (IOException ex) {
                                logger.error(ex.getMessage(), ex);
                        }
                        try {
                                if (httpClient != null) {
                                        httpClient.close();
                                }
                        } catch (IOException ex) {
                                logger.error(ex.getMessage(), ex);
                        }
                        logger.debug(url + " JettyClient send end!");
                        return rs;
                }
        }

        //get提交，返回content
        public String send(String url) {
                logger.debug(url + " JettyClient send begin!");
                String rs = "";
                CloseableHttpClient httpClient = null;
                CloseableHttpResponse response = null;
                try {
                        httpClient = HttpClients.createDefault();

//        RequestConfig config = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT).build();
//        CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(config).build();
                        HttpGet httpget = new HttpGet(url);
                        logger.debug(url + " sending!wait for record!");
                        response = httpClient.execute(httpget);
                        logger.debug(url + " sending finished!");
                        StatusLine st = response.getStatusLine();
                        if (st.getStatusCode() == 200) {
                                logger.debug(url + " JettyClient send getStatusCode is 200!");
                                rs = HttpResponseParser.getResponseContent(response);
                        } else {
                                logger.debug(url + " JettyClient send getStatusCode is not 200!");
                                rs = "";
                        }
                } catch (Exception ex) {
                        logger.error(url + " JettyClient send error!" + ex.getMessage() + "|" + ex.getLocalizedMessage());
                        return "";
                } finally {
                        try {
                                if (response != null) {
                                        response.close();
                                }
                        } catch (IOException ex) {
                                logger.error(ex.getMessage(), ex);
                        }
                        try {
                                if (httpClient != null) {
                                        httpClient.close();
                                }
                        } catch (IOException ex) {
                                logger.error(ex.getMessage(), ex);
                        }
                        logger.debug(url + " JettyClient send end!");
                        return rs;
                }
        }
}
